home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!sun2!ua302aa
- From: ua302aa@sun2.lrz-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: Array Parameters
- Date: 3 Jan 1996 11:12:13 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4cdoad$4v2@sparcserver.lrz-muenchen.de>
- References: <wayne.820650643@hawk>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- wayne@adied.oz.au (Wayne Elliott) writes:
-
- >Can anyone fill me in on whats wrong with the following snippet of
- >code. Basically I'm trying to pass and use a multi-dimensional
- >array.
-
- >-----------------------------------------------------------------
-
- >/* Test passing of array parameters */
-
- >#include <stdio.h>
-
- >char map [][8] =
- >{
- > "abcdef",
- > "ghijkl",
- > "mnopqr"
- >};
-
- Please note that map is an array of unknown size of arrays of 8 chars
-
- >void test(int num, char ** maps)
- >{
- > int i;
- > for(i=0;i<num;i++)
- > if(maps[i])
- > printf("%04d %s\n", i, maps[i]);
- > else
- > printf("%04d NULL\n", i);
- >}
-
- >int main()
- >{
- > test(3, (char **) map);
-
- Why do you cast map to a char**? Maybe becuase you got a diagnostic
- message without the cast? The diagnostic message was helpful, but
- you decided to make it go away.
-
- You have at least two possible ways to get a working program:
-
- 1) Define map as an array of pointers to char:
-
- char *map[] = { /* Initializers */ };
-
- 2) Change the definition of test so that it accepts a pointer to
- arrays of 8 chars:
-
- void test(int num, char (* map)[8]);
-
- > return 0;
- >}
-
- >-----------------------------------------------------------------
-
- >At home this only finds the first element (map[0]).
- >At work this gives a core dump.
- >Using pointer arithmetic (maps+i) produced something like
-
- >0000 abcedf
- >0001 ef
- >0002 ghijkl
-
- >It's all rather comfusing!
- >Any clues? How should I be doing this?
-
- Don't confuse pointers and arrays!
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
- | ua302aa@sunmail.lrz-muenchen.de
-